package runesim.shared;
/**
 * Library of random math methods to use.
 * 
 * @author Ben
 */
public class MiscMath {
	
	// This method needs further testing, but will replace
	// direction(int,int,int,int)
	public static int dir(final int srcX, final int srcY, final int destX, final int destY) {
		final int x = destX - srcX;
		final int y = destY - srcY;
		
		final boolean north = y > 0;
		final boolean south = y < 0;
		final boolean east = x > 0;
		final boolean west = x < 0;
		
		final boolean northe = north && east && !west && !south;
		final boolean southe = south && east && !west && !north;
		final boolean northw = north && west && !east && !south;
		final boolean southw = south && west && !east && !north;
		
		if(srcX == destX && srcY == destY)
			return -1; // Same square
			
		else if(north && !northe && !northw)
			return 0; // North
		else if(north && northe && !northw)
			return 2; // North east
		else if(north && northw && !northe)
			return 14; // North west
			
		else if(south && !southe && !southw)
			return 8; // South
		else if(south && southe && !southw)
			return 6; // South east
		else if(south && southw && !southe)
			return 10; // South west
			
		else if(east && !southe && !northe)
			return 4; // East
		else if(west && !southw && !northw)
			return 12; // West
			
		return 1;
	}
	
	/**
	 * Gets the direction between the two given points. Valid directions are
	 * N:0, NE:2, E:4, SE:6, S:8, SW:10, W:12, and NW:14. The invalid (in
	 * between) direction are 1,3,5,7,9,11,13,15 i.e. odd integers returns -1,
	 * if <code>src</code> and <code>dest</code> are the same.
	 * 
	 * @deprecated Method has been replaced with
	 *             {@link #dir(int, int, int, int)}, as it is quicker and
	 *             cleaner.
	 * @param srcX
	 *            Source X.
	 * @param srcY
	 *            Source Y.
	 * @param destX
	 *            Destination X.
	 * @param destY
	 *            Destination Y.
	 * @return The direction from <code>src</code> to <code>dest</code>.
	 */
	@Deprecated
	public static int direction(int srcX, int srcY, int destX, int destY) {
		int dx = destX - srcX, dy = destY - srcY;
		
		if(dx < 0) {
			if(dy < 0) {
				if(dx < dy)
					return 11;
				else if(dx > dy)
					return 9;
				else
					return 10;
			} else if(dy > 0) {
				if(-dx < dy)
					return 15;
				else if(-dx > dy)
					return 13;
				else
					return 14;
			} else
				return 12;
		} else if(dx > 0) {
			if(dy < 0) {
				if(dx < -dy) {
					return 7;
				} else if(dx > -dy) {
					return 5;
				} else {
					return 6;
				} // dx == -dy
			} else if(dy > 0) {
				if(dx < dy) {
					return 1;
				} else if(dx > dy) {
					return 3;
				} else {
					return 2;
				} // dx == dy
			} else { // dy == 0
				return 4;
			}
		} else { // dx == 0
			if(dy < 0) {
				return 8;
			} else if(dy > 0) {
				return 0;
			} else { // dy == 0
				return -1; // src and dest are the same
			}
		}
	}
	
	public static byte directionDeltaX[] = new byte[] { 0, 1, 1, 1, 0, -1, -1, -1 };
	public static byte directionDeltaY[] = new byte[] { 1, 1, 0, -1, -1, -1, 0, 1 };
	
	public static byte xlateDirectionToClient[] = new byte[] { 1, 2, 4, 7, 6, 5, 3, 0 };
}
